package com.yiran.dao;

import java.sql.*;
import java.util.*;

import com.yiran.model.OrdersClass;
import com.yiran.model.OrdersClass;
import com.yiran.model.OrdersDetailClass;
import com.yiran.util.DBConnection;

public class OrdersBean {
	public int orderId;
	private Connection con;
	OrdersClass order = new OrdersClass();

	public OrdersBean() {
		try {
			con = DBConnection.getConnection();
			System.out.println("conn: " + con);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void setOrderInfo(OrdersClass order) {
		this.order = order;
	}

	private OrdersClass getOrderByNameAndDate(String usersName, String date){
		OrdersClass odr = new OrdersClass();
		try {
		Statement statement = con.createStatement();
		ResultSet resultset = statement.executeQuery("select * from orders where usersName='"+ usersName + "' and ordersDatetime='"+ date+"'");
		while (resultset.next()) {
			odr = new OrdersClass();
			int id = resultset.getInt(1);
			String usersNames = resultset.getString(2);
			String ordersPrice = resultset.getString(3);
			String ordersDatetime = resultset.getString(4);
			int orderFlag = resultset.getInt(5);
			String addr = resultset.getString(6);

			odr.setId(id);
			odr.setUserName(usersNames);
			odr.setOrdersPrice(ordersPrice);
			odr.setOrdersDatetime(ordersDatetime);
			odr.setOrderFlag(orderFlag);
			odr.setAddr(addr);
			return odr;
		}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return odr;
	}
	
	
	public void addOrderInfo() throws Exception {
		try {

			PreparedStatement stm = con
					.prepareStatement("insert into orders(usersName, ordersPrice,ordersDatetime,orderFlag, ordersAddr) values(?,?,?,?,?)");
			System.out.print(order.getUserName() + "\t" + order.getOrdersPrice() + "\t" + order.getOrdersDatetime());
			stm.setString(1, order.getUserName());
			stm.setString(2, order.getOrdersPrice());
			stm.setString(3, order.getOrdersDatetime());
			stm.setInt(4, 0);
			stm.setString(5, order.getOrdersAddr());
			stm.executeUpdate();
			
			OrdersClass orders = getOrderByNameAndDate(order.getUserName(), order.getOrdersDatetime());
			stm = con.prepareStatement("insert into ordersDetails(goodsName, goodsPrice, goodsCount) values(?,?,?)");
			ArrayList list = order.getItems();
			for (Iterator<OrdersDetailClass> it = list.iterator(); it.hasNext();) {
				OrdersDetailClass ci = it.next();
				stm.setString(1, ci.getGoodsName());
				stm.setString(2, ci.getGoodsPrice());
				stm.setInt(3, ci.getGoodsCount());
				stm.executeUpdate();
			}
			orderId = orders.getId();
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	public Collection getAllOrderInfo() {
		OrdersClass order;
		ArrayList arraylist = new ArrayList();
		try {
			Statement statement = con.createStatement();
			ResultSet resultset = statement.executeQuery("select * from orders");

			while (resultset.next()) {
				order = new OrdersClass();
				int id = resultset.getInt(1);
				String usersName = resultset.getString(2);
				String ordersPrice = resultset.getString(3);
				String ordersDatetime = resultset.getString(3);
				String addr = resultset.getString(3);

				order.setId(id);
				order.setUserName(usersName);
				order.setOrdersPrice(ordersPrice);
				order.setAddr(addr);
				arraylist.add(order);
			}
			con.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return arraylist;
	}

	public void deleteOrder(String s) {
		try {
			int i = Integer.parseInt(s);
			Statement statement = con.createStatement();
			statement.execute("delete from orders where id='" + i + "'");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
